package com.zmj.sy.mom.srv.aps.controller;


import com.zmj.sy.mom.srv.aps.bean.entity.order.Order;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ReverseOrder;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.reverseorder.*;
import com.zmj.sy.mom.srv.aps.mapper.OrderMapper;
import com.zmj.sy.mom.srv.aps.mapper.ReverseOrderMapper;
import com.zmj.sy.mom.srv.aps.service.ReverseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RequiredArgsConstructor
@Api(tags = "订单倒排拆包")
@RestController
@RequestMapping("/orderSplit")
public class ReverseOrderController {

    private final ReverseOrderService reverseOrderService;
    private final ReverseOrderMapper reverseOrderMapper;
    private final OrderMapper orderMapper;

    @ApiOperation("2.2.5 订单倒排拆包-列表")
    @GetMapping("/list")
    public BaseListResVo<ReverseOrderListResVo> list(BaseIdReqVo reqVo) {
        return reverseOrderService.list(reqVo);
    }

    @ApiOperation("2.2.6 订单倒排拆包-倒排")
    @PostMapping("/add")
    public BaseResVo add(@RequestBody ReverseOrderAddReqVo reqVo) {
        reverseOrderService.add(reqVo, new HashMap<>());
        return BaseResVo.ok();
    }

    @ApiOperation("2.2.7 订单倒排拆包-负荷")
    @GetMapping("/loads")
    public ReverseOrderLoadsResVo loads(ReverseOrderLoadsReqVo reqVo) {

        ReverseOrder reverseOrder = reverseOrderMapper.selectById(reqVo.getId());
        Order order = orderMapper.selectById(reverseOrder.getOrderId());

        return reverseOrderService.loads(reqVo, 1, order);
    }

    @ApiOperation("2.2.7 订单倒排拆包-负荷明细")
    @GetMapping("/loadsDetail")
    public BaseListResVo<ReverseOrderLoadsDetailResVo> loadsDetail(ReverseOrderLoadsDetailReqVo reqVo) {
        return reverseOrderService.loadsDetail(reqVo);
    }

    @ApiOperation("2.2.8 订单倒排拆包-工单详情-左侧树")
    @GetMapping("/bom")
    public BaseListResVo<ReverseOrderBomResVo> bom(BaseIdReqVo reqVo) {
        return reverseOrderService.bom(reqVo);
    }

    @ApiOperation("2.2.9 订单倒排拆包-工单详情-右侧列表")
    @GetMapping("/bomList")
    public BaseListResVo<ReverseOrderLoadsDetailResVo> bomList(ReverseOrderBomListReqVo reqVo) {
        return reverseOrderService.bomList(reqVo);
    }

    @ApiOperation("2.2.10 订单倒排拆包-移入排产")
    @GetMapping("/moveAps")
    public BaseResVo moveAps(BaseIdReqVo reqVo) {
        reverseOrderService.moveAps(reqVo);
        return BaseResVo.ok();
    }

    @Deprecated
    @ApiOperation("2.2.11 订单倒排拆包-移出排产")
    @GetMapping("/unMoveAps")
    public BaseResVo unMoveAps(BaseIdReqVo reqVo) {
        reverseOrderService.unMoveAps(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("2.2.12 根据包ID获取所有子包ID")
    @GetMapping("/splitDetail")
    public BaseSelectResVo splitDetail(BaseIdReqVo reqVo) {
        return reverseOrderService.splitDetail(reqVo);
    }

    @ApiOperation("智能拆包-负荷图")
    @GetMapping("/autoSplit")
    public ReverseOrderLoadsResVo autoSplit(BaseIdReqVo reqVo) {

        // 判断是一个包还是多个包，如果是0到1个包，则可以进入智能拆包页面
        return reverseOrderService.autoSplit(reqVo.getId());
    }

    @ApiOperation("智能拆包-修改拆包数量")
    @PostMapping("/autoEdit")
    public BaseResVo autoEdit(@RequestBody ReverseOrderAutoEditReqVo reqVo){
        reverseOrderService.autoEdit(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("智能拆包-修改拆包数量")
    @PostMapping("/autoEditMbd")
    public BaseResVo autoEditMbd(@RequestBody ReverseOrderAutoEditReqVo reqVo){
        reverseOrderService.autoEditMbd(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("拆包预览")
    @GetMapping("/splitPreview")
    public BaseListResVo<ReverseOrderSplitPreviewResVo> splitPreview(ReverseOrderSplitPreviewReqVo reqVo) {
        return reverseOrderService.splitPreview(reqVo);
    }


}
